<?php
class AdmininfoAction extends AdminAction {
	/**
	 * 跳转到管理员信息修改页面
	 * @author 沈威
	 * @tel 15005164276
	 * @email 403382132@qq.com
	 */
	public function index(){
		$admin = M('User');
		$uid=getUserInfo('uid',false);
		$map['uid'] = $uid;
		$info = $admin->where($map)->find();
		$info['birth']=substr(date("Y-m-d",$info['birth']),0,10);
		$this->assign('info',$info);
		$this->display();
	}
	
	/**
	 * 修改管理员信息
	 * @author 沈威
	 * @tel 15005164276
	 * @email 403382132@qq.com
	 */
	public function editinfo(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$uid=getUserInfo('uid',false);
		if($uid){
			if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
			{
				$cond = $_POST;
				$cond['uid'] = $uid; 
				$cond['birth'] = strtotime($_POST['birth']);
				$cond['_column'] = array('uid','address','birth','nickname','email','sex','tel');
				foreach ($cond['_column'] as $one) $data[$one] = trim($cond[$one]);
				$user = D('User');
				if($user->create($data)){
					if($user->save($data)){ // 保存当前数据对象
						$this->success("修改成功!!!");
					}else{
						$this->success("无编辑!!!");
					}
				}else{
					$this->error($user->getError());
				}
			}
		}
		//$this->display();
	}
	
	/**
	 * 跳转到修改管理员密码页面
	 * @author 沈威
	 * @tel 15005164276
	 * @email 403382132@qq.com
	 */
	public function editpassword(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$this->display();
	}
	
	/**
	 * 修改管理员密码
	 * @author 沈威
	 * @tel 15005164276
	 * @email 403382132@qq.com
	 */
	public function doeditpassword(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$uid=getUserInfo('uid',false);
		if($uid)
		{
			if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
			{
				$cond = $_POST;
				$cond['uid'] = $uid; 
				$ypassword=md5($_POST['ypassword']);
				$user = D('User');
				$spassword=$user->where('uid='.$uid)->getField('password');
				if($_POST['npassword']!=$_POST['qpassword'])
				{
					$this->error("您输入的两次新密码不一致,请重新输入!");
				}elseif(!$ypassword)
				{
					$this->error("原始密码不能为空!");
				}
				elseif($ypassword!=$spassword)
				{
					$this->error("您输入的原始密码错误,请重新输入!");
				}
				else 
				{
					$cond['uid']=$uid;
					$cond['password'] = md5($_POST['npassword']);
					$cond['_column'] = array('uid','password');
					foreach ($cond['_column'] as $one) $data[$one] = trim($cond[$one]);
					if($user->save($data)){ // 保存当前数据对象
						$this->success("修改成功!!!");
					}else{
						$this->error("修改失败!!!");
					}
				}
			}
		}
	}	
}